{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "411c59b3-f177-4a10-8925-d931ce572eaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import torch\n",
    "from diffusers import StableDiffusionPipeline, DDIMScheduler, AutoencoderKL\n",
    "\n",
    "import w_plus_adapter\n",
    "from script.utils_direction import *\n",
    "import os.path as osp\n",
    "\n",
    "\n",
    "'''\n",
    "model parameter settings\n",
    "'''\n",
    "base_model_path = \"runwayml/stable-diffusion-v1-5\"\n",
    "# base_model_path = 'dreamlike-art/dreamlike-anime-1.0' #animate model\n",
    "# base_model_path = 'darkstorm2150/Protogen_x3.4_Official_Release'\n",
    "\n",
    "vae_model_path = \"stabilityai/sd-vae-ft-mse\"\n",
    "device = \"cuda\"\n",
    "wp_ckpt = './pretrain_models/wplus_adapter_stage1.bin'\n",
    "\n",
    "noise_scheduler = DDIMScheduler(\n",
    "    num_train_timesteps=1000,\n",
    "    beta_start=0.00085,\n",
    "    beta_end=0.012,\n",
    "    beta_schedule=\"scaled_linear\",\n",
    "    clip_sample=False,\n",
    "    set_alpha_to_one=False,\n",
    "    steps_offset=1,\n",
    ")\n",
    "vae = AutoencoderKL.from_pretrained(vae_model_path).to(dtype=torch.float16)\n",
    "pipe = StableDiffusionPipeline.from_pretrained(\n",
    "    base_model_path,\n",
    "    torch_dtype=torch.float16,\n",
    "    scheduler=noise_scheduler,\n",
    "    vae=vae,\n",
    "    feature_extractor=None,\n",
    "    safety_checker=None\n",
    ")\n",
    "\n",
    "if not osp.exists('./pretrain_models/wplus_adapter_stage1.bin'):\n",
    "    download_url = 'https://github.com/csxmli2016/w-plus-adapter/releases/download/v1/wplus_adapter_stage1.bin'\n",
    "    load_file_from_url(url=download_url, model_dir='./pretrain_models/', progress=True, file_name=None)\n",
    "\n",
    "wp_model = w_plus_adapter.WPlusAdapter(pipe, wp_ckpt, device)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8081d92-8f42-4bcd-9f83-44aec3f549a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "'''\n",
    "Parameter settings\n",
    "'''\n",
    "\n",
    "which_w_direction = 'interfacegan' # interfacegan, ganspace, latentdirection # \n",
    "prompt = 'a photo of a face'\n",
    "seed = 18\n",
    "\n",
    "e4e_id_path = './test_data/stage1/1.pth'\n",
    "\n",
    "imgs = []\n",
    "direction_length = 4\n",
    "residual_att_scale = 0.8\n",
    "use_freeu = True\n",
    "\n",
    "scales = list(range(-direction_length, direction_length, 2))\n",
    "\n",
    "id_noise0 = torch.load(e4e_id_path, map_location='cpu')\n",
    "\n",
    "for i in scales:\n",
    "    if which_w_direction == 'interfacegan':\n",
    "        att = 'smile' # age, smile\n",
    "        direction = get_direction_from_interfacegan(att)\n",
    "        id_noise = id_noise0 + i * direction\n",
    "\n",
    "    if which_w_direction == 'ganspace':\n",
    "        att = 'eyes_open' #eyes_open, emotion_angry, emotion_disgust, emotion_fear, emotion_sad, emotion_happy, emotion_surprise,  gender, width\n",
    "        direction = get_direction_from_latentdirection(att)\n",
    "        id_noise[:, :8, ...] = (id_noise0 + i * direction)[:, :8, ...]\n",
    "\n",
    "\n",
    "    if which_w_direction == 'latentdirection':\n",
    "        att = 'lipstick' #big_smile, face_roundness, lipstick, overexposed, short_face, smile\n",
    "        id_noise = id_noise0 + get_direction_from_ganspace(att, id_noise0, i) \n",
    "    \n",
    "    images = wp_model.generate_idnoise(prompt=prompt, w=id_noise.repeat(1, 1, 1).to(device, torch.float16), scale=residual_att_scale, num_samples=1, num_inference_steps=50, seed=seed, use_freeu=use_freeu, negative_prompt=None)\n",
    "\n",
    "    imgs += images\n",
    "\n",
    "\n",
    "grid = image_grid(imgs, 1, len(imgs))\n",
    "display(grid)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30ee4b03",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
